package com.wy.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Auther: tracy
 * @Date: 2018/12/4 18:12
 * @Description:
 */
public class ImageUtils {
    /**
     * 转换图片格式
     * @param srcFileName
     * @param tarPath
     * @return
     */
    public static String convertJPG(String srcFileName,String tarPath) {
        BufferedImage bufferedImage;
        try {
            //read image file
            bufferedImage = ImageIO.read(new File(srcFileName));
            // create a blank, RGB, same width and height, and a white background
            BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
                    bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
            //TYPE_INT_RGB:创建一个RBG图像，24位深度，成功将32位图转化成24位
            newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
            // write to jpeg file
            ImageIO.write(newBufferedImage, "jpg", new File(tarPath));
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tarPath;
    }
}
